UdkpOptions = {["UdkpButtonPosition"]=310, ["UdkpButtonShown"] = true, ["ItemQuality"] = 2,["UdkpChannelLevel"]="RAID",["Rdkp"]=1};
Udkp_SortOptions = {["method"]="", ["itemway"]=""};
Uasit_raidteamid="";
Uasit_DebugFlag=false;

function UdkpButton_OnClick()
	udkp_udkp_show();
end

udkp_udkp_show=function() 
	if(UDkpMainFrame:IsVisible()) then
		HideUIPanel(UDkpMainFrame);
	else
		ShowUIPanel(UDkpMainFrame);
	end;
end;



function UdkpButton_UpdatePosition()
	UDkpButtonFrame:SetPoint(
		"TOPLEFT",
		"Minimap",
		"TOPLEFT",
		54 - (78 * cos(UdkpOptions.UdkpButtonPosition)),
		(78 * sin(UdkpOptions.UdkpButtonPosition)) - 55
	);
end

local firstShow = true;

function UDKPMain_SelectTab(tab)
	PanelTemplates_SetTab(UDkpMainFrame, tab);
	local TabFrames = {
			[1] = "Udkp_RaidMainFrame",
			[2] = "Udkp_MemberDkpFrame",
			[3] = "Udkp_Set"
		};
		for id, name in pairs(TabFrames) do
		if (id == tab) then
			getglobal(name):Show();
		else
			getglobal(name):Hide();
		end
	end
	
end

function UDKPMain_OnMouseUp()
	if ( UDkpMainFrame.isMoving ) then
		UDkpMainFrame:StopMovingOrSizing();
		UDkpMainFrame.isMoving = false;
	end
end

function UDKPMain_OnMouseDown(button)
	if ( ( ( not UDkpMainFrame.isLocked ) or ( UDkpMainFrame.isLocked == 0 ) ) and ( arg1 == "LeftButton" ) ) then
		UDkpMainFrame:StartMoving();
		UDkpMainFrame.isMoving = true;
	end
end

function UDKPMain_OnHide()
	if ( UDkpMainFrame.isMoving ) then
		UDkpMainFrame:StopMovingOrSizing();
		UDkpMainFrame.isMoving = false;
	end
end

function UDKPMain_OnShow()
	if (firstShow) then
		UDKPMain_SelectTab(1);
		firstShow = false;
	end;
	getglobal("UDkpMainFrameTitle"):SetText(UDKP_TITLE.."V"..UDkpVersion)
	
end

function UDKPMain_OnLoad()
	tinsert(UISpecialFrames,"UDkpMainFrame");
	--tinsert(UISpecialFrames,"Udkp_ItemFrame");

	PanelTemplates_SetNumTabs(UDkpMainFrame, 3);
	--UDKPMain_SelectTab(1);
	PanelTemplates_SetTab(UDkpMainFrame, 1);
	
	
end

function UDKP_Toggle(page)
  if (UDkpMainFrame:IsVisible()) then
    UDkpMainFrame:Hide();
  else
    if(page)
    then
      firstShow = false;
      UDKPMain_SelectTab(page);
    end
    UDkpMainFrame:Show();
  end
end


-------- HELP ------
function UDKP_Help_SetTooltip()
	local uiX, uiY = UIParent:GetCenter();
	local thisX, thisY = this:GetCenter();

	local anchor = "";
	if ( thisY > uiY ) then
		anchor = "BOTTOM";
	else
		anchor = "TOP";
	end
	
	if ( thisX < uiX  ) then
		if ( anchor == "TOP" ) then
			anchor = "TOPLEFT";
		else
			anchor = "BOTTOMRIGHT";
		end
	else
		if ( anchor == "TOP" ) then
			anchor = "TOPRIGHT";
		else
			anchor = "BOTTOMLEFT";
		end
	end
	GameTooltip:SetOwner(this, "ANCHOR_" .. anchor);
end

function UDKP_Help_LoadText()
 local texts = {
    GEM_HELP_EVENTS_TAB_EVENTS,
    GEM_HELP_PLAYERS_TAB_MEMBERS,
    GEM_HELP_NEW_TAB_TEMPLATES,
    GEM_HELP_NEW_TAB_SORTING,
    GEM_HELP_NEW_TAB_CONFIG,
  };

  this.text = texts[this:GetID()];
end

function Uasit_Debug(...)
	if ( Uasit_DebugFlag ) then
		local sDebug = "#";
		for i = 1, select("#", ...), 1 do
			if ( select(i, ...) ) then
				sDebug = sDebug .. select(i, ...) .. "#";
			end
		end
		DEFAULT_CHAT_FRAME:AddMessage(sDebug, 1, 0.5, 0);
	end
end

udkp_initialize = function()
		SlashCmdList["SHOW_UDKP"] = udkp_udkp_show;
		SLASH_SHOW_UDKP1 = "/udkp";
end;



function Udkp_FixZero(num)
	if ( num < 10 ) then
		return "0" .. num;
	else
		return num;
	end
end

function Udkp_Date()
	local t = date("*t");

	return strsub(t.year, 3).."-" ..Udkp_FixZero(t.month) .. "-" .. Udkp_FixZero(t.day) ..   " " .. Udkp_FixZero(t.hour) .. ":" .. Udkp_FixZero(t.min) .. ":" .. Udkp_FixZero(t.sec);
end


function Udkp_TeamDropDown_OnLoad()
 	UIDropDownMenu_Initialize(this, Udkp_Team_Init);
 	
 	
 	if(Uasit_raidteamid=="") then
 		if(UDKP_MEMBER_DKP) then
			totalteam=getn(UDKP_MEMBER_DKP);
			if(totalteam>0) then
				Uasit_Debug("Udkp_TeamDropDown_OnLoad teamid"..UDKP_MEMBER_DKP[1]["teamid"])
				UIDropDownMenu_SetSelectedValue(this, UDKP_MEMBER_DKP[1]["teamid"]);
				Uasit_raidteamid=UDKP_MEMBER_DKP[1]["teamid"]
			end
		end
 	else
 		UIDropDownMenu_SetSelectedValue(this, Uasit_raidteamid);	
 	end
 	
end

function Udkp_Team_Init()
	if(UDKP_MEMBER_DKP) then
		totalteam=getn(UDKP_MEMBER_DKP);
		for i=1,totalteam do
			info = {};
			info.text = UDKP_MEMBER_DKP[i]["teamname"];
			info.value = UDKP_MEMBER_DKP[i]["teamid"];
			info.func = Udkp_TeamDropDown_OnClick;
			UIDropDownMenu_AddButton(info);
		end
	end
end

function Udkp_TeamDropDown_OnClick()
	UIDropDownMenu_SetSelectedValue(Udkp_TeamDropDown, this.value);
	Uasit_raidteamid=this.value;
	if(TEST_PARTY_STUFF and getn(TEST_PARTY_STUFF) > 0) then
		TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Team"]=Uasit_raidteamid;
		totalMembers = getn(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"]);
		for i = 1,totalMembers do
			TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["pdkp"]=getUdkpPlayerDKPLeft(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"][i]["player"]);
		end
		Uasit_UpdateMember();
	end
	local t=date("*t");
	UdkpRaidName_Edit:SetText(t.month.."-"..t.day..Udkp_RaidMap_Edit:GetText().."("..getCurrentTeamName()..")");
	
end

function Udkp_setButtonWidth(width,ele)
	ele:SetWidth(width);
	getglobal(ele:GetName().."Middle"):SetWidth(width - 4 - 4);
end